package com.example.demo.scheduler;

import com.example.demo.mapper.StudentCouponsMapper;
import com.example.demo.mapper.entity.Coupons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDate;
import java.util.List;

/**
 * @author Lucy
 * @create 2024-02-25 11:56
 */
@Component
public class CouponScheduler {

    @Autowired
    private StudentCouponsMapper couponsMapper;

    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行一次
    //@Scheduled(cron = "0 * * * * ?") // 每分钟执行一次
    @Transactional
    public void expireCoupons() {
        // 查询过期的优惠券
        List<Coupons> expiredCoupons = couponsMapper.selectExpiredCoupons();

        if(expiredCoupons.size() != 0){
            // 更新过期优惠券的状态
            for (Coupons coupons : expiredCoupons) {
                couponsMapper.updateExpiredStudentCoupons(coupons.getCouponsId());
                couponsMapper.updateExpiredCoupons(coupons.getCouponsId());
            }
        }
    }
}